package jumpingnotes.dao.hibernate;

import java.util.List;

import jumpingnotes.dao.BookCommentDao;
import jumpingnotes.model.entity.BookComment;
import net.jf.lucene.analysis.mmseg.MMSegAnalyzer;

import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.ParseException;
import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.hibernate.search.FullTextSession;
import org.hibernate.search.Search;
import org.springframework.orm.hibernate3.HibernateCallback;

public class BookCommentHibernateDao extends GenericHibernateDao<BookComment> implements BookCommentDao
{
	public BookCommentHibernateDao()
	{
		super(BookComment.class);
	}

	@SuppressWarnings("unchecked")
	public List<BookComment> findBookCommentByBookId(final int bookId, final int firstResult, final int maxResults)
	{
		return (List<BookComment>) getHibernateTemplate().execute(new HibernateCallback()
		{
			public Object doInHibernate(Session session) throws HibernateException
			{
				Criteria criteria = session.createCriteria(BookComment.class);
				criteria.createAlias("book", "book");
				criteria.add(Restrictions.eq("book.bookId", bookId));
				criteria.setFetchMode("member", FetchMode.JOIN);

				if (firstResult >= 0)
					criteria.setFirstResult(firstResult);
				if (maxResults > 0)
					criteria.setMaxResults(maxResults);

				return criteria.list();
			}
		});
	}

	@Override
	public BookComment findById(final int bookCommentId)
	{
		return (BookComment) getHibernateTemplate().execute(new HibernateCallback()
		{
			public Object doInHibernate(Session session) throws HibernateException
			{
				Criteria criteria = session.createCriteria(BookComment.class);
				criteria.add(Restrictions.eq("bookCommentId", bookCommentId));
				criteria.setFetchMode("member", FetchMode.JOIN);
				return criteria.uniqueResult();
			}
		});
	}

	@SuppressWarnings("unchecked")
	public void indexBookComment()
	{
		Session session = getSession();
		FullTextSession fullTextSession = Search.createFullTextSession(session);

		List<BookComment> bookCommentList = session.createCriteria(BookComment.class).list();
		for (BookComment bookComment : bookCommentList)
		{
			fullTextSession.index(bookComment);
		}
	}

	@SuppressWarnings("unchecked")
	public List<BookComment> searchBookComment(String keyword, int firstResult, int maxResults)
	{
		Session session = getSession();
		FullTextSession fullTextSession = Search.createFullTextSession(session);

		try
		{
			MultiFieldQueryParser parser = new MultiFieldQueryParser(new String[] {
				"body"
			}, new MMSegAnalyzer());
			org.apache.lucene.search.Query query = parser.parse(keyword);
			Query hibQuery = fullTextSession.createFullTextQuery(query, BookComment.class);

			if (firstResult >= 0)
				hibQuery.setFirstResult(firstResult);
			if (maxResults > 0)
				hibQuery.setMaxResults(maxResults);

			List<BookComment> result = hibQuery.list();
			return result;
		}
		catch (ParseException e)
		{
			e.printStackTrace();
			return null;
		}
	}
}
